package cn.sharetraffic.mi.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import cn.sharetraffic.mi.CustomerManager;
import cn.sharetraffic.mi.PushApiManager;
import org.apache.log4j.Logger;


import cn.jpush.api.push.model.Message;

/**
 * 服务器启动后全局监听
 */
public class SystemListener implements ServletContextListener {

	public static final CustomerManager customerManager = new CustomerManager();
	private Logger log = Logger.getLogger(getClass());

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		customerManager.setLoop(false);
	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
			public void run() {
				log.warn("服务器程序被Shutdown！");
				PushApiManager.getInstance().pushMessageToAll(Message.content("服务器程序被Shutdown！"));
			}
		}));
		customerManager.start();
		log.warn("服务器启动！");
		PushApiManager.getInstance().pushMessageToAll(Message.content("服务器启动！"));
	}

	public CustomerManager getCustomerManager() {
		return customerManager;
	}

}
